package com.xnxy.design.action;

import com.xnxy.design.model.Goods;
import com.xnxy.design.model.User;
import com.xnxy.design.service.CoreService;
import com.xnxy.design.service.UserService;
import com.xnxy.design.util.PageData;
import com.xnxy.design.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserAction extends CoreController<User, Integer> {

    @Autowired
    UserService userService;

    @Override
    public CoreService<User, Integer> getService() {
        return userService;
    }

    @RequestMapping("/test")
    private ModelAndView test(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("hello");
        PageData pageData = queryData(request);
        modelAndView.addObject("array", pageData.getListData());
        return modelAndView;
    }

    @RequestMapping("/user/admin/isUser")
    @ResponseBody
    private Map isUser() {
        Map map=new HashMap();
        if (SessionUtil.getUser()==null){
            map.put("msg",false);
        }else {
            map.put("msg",true);
            map.put("data", SessionUtil.getUser());
        }
        return map;
    }


    @RequestMapping("/isLogin")
    private ModelAndView isLogin(HttpServletRequest request,HttpServletResponse response, String username, String pswd) throws IOException {
        boolean flag=userService.isLogin(username,pswd);

        if (flag){
            HttpSession session = (request).getSession();
            session.setAttribute("user",userService.getByusername(username));
            response.sendRedirect("index");
            return null;
        }else {
            ModelAndView modelAndView = new ModelAndView("login");
            modelAndView.addObject("islogin","登录验证失败");
            return modelAndView;
        }
    }


    @RequestMapping("/user/admin/isLogin")
    @ResponseBody
    private Map<String,Object> adminLogin(HttpServletRequest request,HttpServletResponse response, String username, String password) throws IOException {
        User user=userService.getByusername(username);
        if (password.equals(user.getPassword())&&user.getType()==1){
            HttpSession session = (request).getSession();
            session.setAttribute("user",userService.getByusername(username));
            return success(true,"登录成功");
        }else {
            return success(false,"登录失败");
        }
    }


    @RequestMapping("/exit")
    @ResponseBody
    public Map<String,Object> exit(HttpSession session){
        session.setAttribute("user",null);
        return success(true,"退出成功");
    }

    @RequestMapping("/user/inster")
    @ResponseBody
    public Map<String,Object> inster(User user){
        if (user.getType()==null){
            user.setType(0);
        }
        if (user.getBalance()==null){
            user.setBalance(0.0);
        }
        if (user.getUsername()==null||"".equals(user.getUsername())){
            return success(false,"注册失败，请输入用户名");
        }
        if (user.getPassword()==null||"".equals(user.getPassword())){
            return success(false,"注册失败，请输入密码");
        }
        if (user.getZfpassword()==null||"".equals(user.getZfpassword())){
            return success(false,"注册失败，请输入支付密码");
        }
        if (user.getId()==null){
            userService.insert(user);
        }else {
            userService.updata(user);
        }
        return success(true,"请求成功");
    }

    @RequestMapping("/admin/user/list")
    @ResponseBody
    public Map<String,Object> list(){
        List list=selectAll();
        return success(true,"查询成功",list);
    }

    @RequestMapping("/admin/user/delect")
    @ResponseBody
    public Map<String,Object> delectUser(Integer id[]){
        Boolean flag=delect(id);
        if (flag){
            return success(true,"删除成功");
        }else {
            return success(false,"删除失败");
        }

    }

    @RequestMapping("/admin/user/getUser")
    @ResponseBody
    public Map<String,Object> getUser(Integer id){
        User user=selectByPrimaryKey(id);
        return success(true,"请求成功",user);
    }


    @RequestMapping("/isUserAdmin")
    private ModelAndView isUserAdmin(Integer type) {
        ModelAndView modelAndView = new ModelAndView("userAdmin");
        modelAndView.addObject("type",type);
        return modelAndView;
    }


    @RequestMapping("/user/updata")
    public ModelAndView update(User user){
        ModelAndView modelAndView = new ModelAndView("userAdmin");
        userService.updata(user);
        modelAndView.addObject("type",1);
        return modelAndView;
    }

}
